#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
struct SuffixArray
{
typedef long long ll;
#define N 1000005
char s[N];
int n,m,par[N],sz[N],cur[N];
int *x,*y,X[N],Y[N],c[N],sa[N],height[N],Rank[N];
ll ans;
vector<int>add[N];
inline int find(int x){
return par[x]==x?x:par[x]=find(par[x]);
}
inline void clear()
{
memset(X,0,sizeof(X));memset(Y,0,sizeof(Y));memset(c,0,sizeof(c));
memset(sa,0,sizeof(sa));memset(height,0,sizeof(height));memset(Rank,0,sizeof(Rank));
}
inline void init()
{
ans=0;
scanf("%d%s",&n,s);
for(int i=0;i<=n;++i){
par[i]=i;
sz[i]=1;
}
cur[1]=n+1;
}
inline void get_sa(int _m=30)//_m为字符集大小
{
m=_m;
x=X,y=Y;
for (int i=0;i<m;++i) c[i]=0;
for (int i=0;i<n;++i) x[i]=s[i]-'a',++c[x[i]];
for (int i=1;i<m;++i) c[i]+=c[i-1];
for (int i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
for (int k=1;k<=n;k<<=1)
{
int p=0;
for (int i=n-k;i<n;++i) y[p++]=i;
for (int i=0;i<n;++i) if (sa[i]>=k) y[p++]=sa[i]-k;
for (int i=0;i<m;++i) c[i]=0;
for (int i=0;i<n;++i) ++c[x[y[i]]];
for (int i=1;i<m;++i) c[i]+=c[i-1];
for (int i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for (int i=1;i<n;++i)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&((sa[i-1]+k<n?y[sa[i-1]+k]:-1)==(sa[i]+k<n?y[sa[i]+k]:-1))?p-1:p++;
if (p>n) break;
m=p;
}
}
inline void get_height()
{
for (int i=0;i<n;++i) Rank[sa[i]]=i;
int k=0;height[0]=0;
for (int i=0;i<n;++i)
{
if (!Rank[i]) continue;
if (k) --k;
int j=sa[Rank[i]-1];
while (i+k<n&&j+k<n&&s[i+k]==s[j+k]) ++k;//必须是原数组s 不能是现数组X/x
height[Rank[i]]=k;
}
}
//Rank[i]:下标位置在i的后缀的排名
//sa[i]:后缀排名第i的下标位置
//Rank和sa互为反函数 范围均在[0,n-1]
//height[i]:排名第i和排名第i-1的LCP长度
void PR()
{
string p(s);
for(int i=0;i<n;++i)
printf("Rank[%d]:%d\n",i,Rank[i]);
for(int i=0;i<n;++i)
{
printf("sa[%d]:%d ",i,sa[i]);
cout<<p.substr(sa[i])<<endl;
}
for(int i=0;i<n;++i)
printf("height[%d]:%d\n",i,height[i]);
}
inline void un(int x,int y){
x=find(x),y=find(y);
if(x==y)return;
cur[sz[x]]--;
cur[sz[y]]--;
par[y]=x;
sz[x]+=sz[y];
cur[sz[x]]++;
}
void solve()
{
init();
get_sa();
get_height();
//PR();
for(int i=1;i<=n;++i){
if(height[i]>=1){
add[height[i]].push_back(i);
}
}
for(int i=n;i>1;--i){
for(auto &x:add[i]){
un(x-1,x);
}
//cout<<"i:"<<i<<endl;
//for(int j=1;j<=n;++j){
// cout<<"cur["<<j<<"]:"<<cur[j]<<endl;
//}
for(int j=i;j<=n;j+=i){
ans+=1ll*j*cur[j];
}
}
cout<<ans+n<<endl;
}
}sa;
int main()
{
sa.solve();
return 0;
}
//1 3 1 >=1 说明a出现了4次
//3 >=3 aba出现了1次 ab出现了1次
122A - Lucky Division | 1611C - Polycarp Recovers the Permutation |
432A - Choosing Teams | 758A - Holiday Of Equality |
1650C - Weight of the System of Nested Segments | 1097A - Gennady and a Card Game |
248A - Cupboards | 1641A - Great Sequence |
1537A - Arithmetic Array | 1370A - Maximum GCD |
149A - Business trip | 34A - Reconnaissance 2 |
59A - Word | 462B - Appleman and Card Game |
1560C - Infinity Table | 1605C - Dominant Character |
1399A - Remove Smallest | 208A - Dubstep |
1581A - CQXYM Count Permutations | 337A - Puzzles |
495A - Digital Counter | 796A - Buying A House |
67A - Partial Teacher | 116A - Tram |
1472B - Fair Division | 1281C - Cut and Paste |
141A - Amusing Joke | 112A - Petya and Strings |
677A - Vanya and Fence | 1621A - Stable Arrangement of Rooks |